package stream;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

/**
 * @author shentianyu
 * @date 2022-06-03 18:31
 */
public class TestForkJoin {
    public static void main(String[] args) {
        Instant start = Instant.now();

        ForkJoinPool pool = new ForkJoinPool();
        ForkJoinTask<Long> forkJoin = new ForkJoin(1,1000000000L);
        System.out.println(pool.invoke(forkJoin));

        Instant end = Instant.now();
        System.out.println(Duration.between(start,end).toMillis());

        Instant start1 = Instant.now();
        System.out.println(LongStream.rangeClosed(0, 1000000000L)
                .parallel()
                .reduce(0, Long::sum));
        Instant end1 = Instant.now();
        System.out.println(Duration.between(start1,end1).toMillis());

    }
}
